package jgl.vajra.operator.part.vehicle.activity;

import android.graphics.Paint;
import android.os.CountDownTimer;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.operator.R;
import jgl.vajra.operator.databinding.ActivityOrderStatusBinding;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.vehicle.contract.OrderStatusContract;
import jgl.vajra.operator.part.vehicle.viewModel.OrderStatusViewModel;
import jgl.vajra.operator.view.NestedViewGroup;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.utils.CommonUtils;

@CreateViewModel(OrderStatusViewModel.class)
public class OrderStatusActivity extends BaseMVVMActivity<OrderStatusViewModel, ActivityOrderStatusBinding> implements OrderStatusContract.View {
    @Override
    public int getLayoutId() {
        return R.layout.activity_order_status;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        mBinding.tvWaitingFee.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        mBinding.tvWaitingFee.getPaint().setAntiAlias(true);//抗锯齿

        timerStart();

        List<String> list = new ArrayList<>();
        list.add("");

      /*  // 不可嵌套滑动
        mBinding.addressRecyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.addressRecyclerView.setFocusableInTouchMode(false);*/
        //设置布局管理器
        mBinding.addressRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        SingleTypeBindingAdapter addressAdapter = new SingleTypeBindingAdapter(this, list, R.layout.item_address_confirm_order);
        mBinding.addressRecyclerView.setAdapter(addressAdapter);


        mBinding.ddViewGroup.setOnScrollListener(new NestedViewGroup.IScrollListener() {
            @Override
            public void onTargetToTopDistance(int distance) {
                Logger.d("target top :" + distance);
                final float total = CommonUtils.getScreenHeight(getApplicationContext())
                        - CommonUtils.getStatusBarHeight(getApplicationContext())
                        - mBinding.ddViewGroup.getTargetInitBottom();
                // 设置标题栏透明度

                float num = 255 * (1 - distance / total);

                if (num > 160) {
                    mBinding.llReceiver.setVisibility(View.GONE);
                } else {
                    mBinding.llReceiver.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onHeaderToTopDistance(int distance) {
                Logger.d("header top :" + distance);
            }
        });

    }

    /**
     * 倒数计时器
     */
    private CountDownTimer timer = new CountDownTimer(10 * 60 * 1000, 1000) {
        /**
         * 固定间隔被调用,就是每隔countDownInterval会回调一次方法onTick
         * @param millisUntilFinished
         */
        @Override
        public void onTick(long millisUntilFinished) {
            mBinding.tvTime.setText(formatTime(millisUntilFinished));
        }

        /**
         * 倒计时完成时被调用
         */
        @Override
        public void onFinish() {
            mBinding.tvTime.setText("00:00");
        }
    };

    /**
     * 将毫秒转化为 分钟：秒 的格式
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        int minute;//分钟
        int second;//秒数
        minute = (int) ((millisecond / 1000) / 60);
        second = (int) ((millisecond / 1000) % 60);
        if (minute < 10) {
            if (second < 10) {
                return "0" + minute + ":" + "0" + second;
            } else {
                return "0" + minute + ":" + second;
            }
        } else {
            if (second < 10) {
                return minute + ":" + "0" + second;
            } else {
                return minute + ":" + second;
            }
        }
    }

    /**
     * 取消倒计时
     */
    public void timerCancel() {
        timer.cancel();
    }

    /**
     * 开始倒计时
     */
    public void timerStart() {
        timer.start();
    }


    @Override
    public void onResume() {
        mBinding.mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mBinding.mapView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mBinding.mapView.onDestroy();
        super.onDestroy();
    }

    //立即支付
    @Override
    public void onImmediatePayment() {
        IntentManager.toImmediatePaymentActivity(this);
    }

    //安全中心
    @Override
    public void onSecurityCenter() {
        IntentManager.toSecurityCenterActivity(this);
    }

    //等候费
    @Override
    public void onWaitingFee() {
        IntentManager.toWaitingFeeActivity(this);
    }

    //报警
    @Override
    public void onPolice() {
        mBinding.tvDetermine.setVisibility(View.GONE);
        mBinding.rlBeforeWork.setVisibility(View.VISIBLE);
        mBinding.rlAfterWork.setVisibility(View.GONE);
        mBinding.llHead.setVisibility(View.GONE);
        mBinding.toolbar.setTitleText("机佬正在赶往吊货地");
        mBinding.tvTitle.setText("大名驾着霸天虎距您的吊货地还有8公里");
    }

    //客服
    @Override
    public void onCustomerService() {
        mBinding.tvDetermine.setVisibility(View.GONE);
        mBinding.rlBeforeWork.setVisibility(View.VISIBLE);
        mBinding.rlAfterWork.setVisibility(View.GONE);
        mBinding.llHead.setVisibility(View.GONE);
        mBinding.llTiming.setVisibility(View.VISIBLE);
        mBinding.toolbar.setTitleText("机佬已到达吊货地");
        mBinding.tvTitle.setText("霸天虎已到达您的吊货地，正在等待中…");
    }

    //分享单子
    @Override
    public void onShareOrder() {
        mBinding.tvDetermine.setVisibility(View.GONE);
        mBinding.rlBeforeWork.setVisibility(View.GONE);
        mBinding.rlAfterWork.setVisibility(View.VISIBLE);
        mBinding.llHead.setVisibility(View.GONE);
        mBinding.toolbar.setTitleText("机佬正在吊货");
        mBinding.tvTitle.setText("大名正在为你打工，霸天虎已开始吊货");
    }

    //取消单子
    @Override
    public void onCancelOrder() {
        mBinding.tvDetermine.setVisibility(View.GONE);
        mBinding.rlBeforeWork.setVisibility(View.GONE);
        mBinding.rlAfterWork.setVisibility(View.VISIBLE);
        mBinding.llHead.setVisibility(View.GONE);
        mBinding.toolbar.setTitleText("机佬正在休息");
        mBinding.tvTitle.setText("大名正在中途休息...");
    }

    //更换机佬
    @Override
    public void onChangeDriver() {
        mBinding.rlBeforeWork.setVisibility(View.GONE);
        mBinding.rlAfterWork.setVisibility(View.VISIBLE);
        mBinding.tvDetermine.setVisibility(View.VISIBLE);
        mBinding.llHead.setVisibility(View.GONE);
        mBinding.toolbar.setTitleText("机佬已完成单子");
        mBinding.tvTitle.setText("大名已完成单子");
    }

    //确认完成
    @Override
    public void onDetermine() {
        IntentManager.toOrderEvaluateActivity(this);
    }

    //视频监控
    @Override
    public void onVideoSurveillance() {
        IntentManager.toVideoSurveillanceActivity(this);

    }
}
